//
//  ViewController.swift
//  Grouping Tasks Together
//
//  Created by Vandad Nahavandipoor on 7/3/14.
//  Copyright (c) 2014 Pixolity Ltd. All rights reserved.
//
//  These example codes are written for O'Reilly's iOS 8 Swift Programming Cookbook
//  If you use these solutions in your apps, you can give attribution to
//  Vandad Nahavandipoor for his work. Feel free to visit my blog
//  at http://vandadnp.wordpress.com for daily tips and tricks in Swift
//  and Objective-C and various other programming languages.
//  
//  You can purchase "iOS 8 Swift Programming Cookbook" from
//  the following URL:
//  http://shop.oreilly.com/product/0636920034254.do
//
//  If you have any questions, you can contact me directly
//  at vandad.np@gmail.com
//  Similarly, if you find an error in these sample codes, simply
//  report them to O'Reilly at the following URL:
//  http://www.oreilly.com/catalog/errata.csp?isbn=0636920034254

import UIKit

class ViewController: UIViewController {
                            
  func reloadTableView(){
    /* Reload the table view here */
    print(__FUNCTION__)
  }
  
  func reloadScrollView(){
    /* Do the work here */
    print(__FUNCTION__)
  }
  
  func reloadImageView(){
    /* Reload the image view here */
    print(__FUNCTION__)
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    let taskGroup =  dispatch_group_create()
    let mainQueue =  dispatch_get_main_queue()
    
    /* Reload the table view on the main queue */
    dispatch_group_async(taskGroup, mainQueue, {[weak self] in
      self!.reloadTableView()
      });
    
    /* Reload the scroll view on the main queue */
    dispatch_group_async(taskGroup, mainQueue, {[weak self] in
      self!.reloadScrollView()
      });
    
    /* Reload the image view on the main queue */
    dispatch_group_async(taskGroup, mainQueue, {[weak self] in
      self!.reloadImageView()
      });
    
    /* At the end when we are done, dispatch the following block */
    dispatch_group_notify(taskGroup, mainQueue, {[weak self] in
      /* Do some processing here */
      
      let controller = UIAlertController(title: "Finished",
        message: "All tasks are finished",
        preferredStyle: .Alert)
      
      
      controller.addAction(UIAlertAction(title: "OK",
        style: .Default,
        handler: nil))
      
      self!.presentViewController(controller, animated: true, completion: nil)
      
      });
    
  }

}

